/**
 *  *2010-7-17 下午01:28:12  add 
 */
package com.base.myproject.client.HR.bcode;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.LasyObejct;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.SWFFileUpload;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.Util;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Administrator 2010-7-17 javazgw@gmail.com
 */
public class Bcode_Extdata extends LayoutContainer implements LasyObejct {

	private LayoutContainer details;
	private XTemplate detailTp;

	Button add = new Button("上传");
	Button del = new Button("删除");

	Button save = new Button("保存");
	BaseStore<BusiObject> mainstore = null;
	public Bcode_Extdata(BaseStore<BusiObject> mainstore) {
		this.mainstore = mainstore;
		init();
	}

	private void init() {
		setLayout(new TableLayout(2));
		detailTp = XTemplate.create(getDetailTemplate());
		// add(CreateView());
		details = new LayoutContainer();
		details.setBorders(true);
		details.setStyleAttribute("backgroundColor", "white");
		
		TableData td1 = new TableData();
		TableData td2 = new TableData();
		TableData td3 = new TableData();

		td1.setColspan(2);
		td2.setColspan(1);
		td3.setColspan(1);
		
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td3.setVerticalAlign(VerticalAlignment.TOP);

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSpacing(5);
		//hPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		hPanel.add(add);
		hPanel.add(del);
		add(hPanel, td1);

		add(CreateView(), td2);
		
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setWidth("320");
		//vpanel.add(details);
		vpanel.add(createForm());
		add(vpanel, td3);

		add.addListener(Events.Select, getListener());
		del.addListener(Events.Select, getListener());
		save.addListener(Events.Select, getListener());
	}

	BaseStore<BusiObject> store = null;
	ListView<BusiObject> view = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("imagepath");
		type.getField("imagepath").setType(String.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("bcode_extdata");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from bcode_extdata where bcode='{bcode}'");
		 info.setParent(mainstore);// 关联主表
		 info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		 info.setLinkparent_chile_columns(new String[] { "bcode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	ContentPanel CreateView() {
		final ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		// panel.setCollapsible(true);
		// panel.setAnimCollapse(false);
		panel.setFrame(true);
		panel.setId("images-view");
		// panel.setHeading("产品图片 (0 个产品被选中)");
//		 panel.setWidth(535);

		// panel.setAutoHeight(true);
//		panel.setHeight(360);
		panel.setBodyBorder(false);

		view = new ListView<BusiObject>() {
			@Override
			protected BusiObject prepareData(BusiObject model) {

				return model;
			}

		};

		view.setTemplate(getTemplate());
		view.setStore(createStore());
//		view.setHeight(300);
//		view.setWidth(500);
		view.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		view.setItemSelector("div.thumb-wrap");
		view.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BusiObject>>() {

					public void handleEvent(SelectionChangedEvent<BusiObject> be) {
						// panel.setHeading("产品图片(" + be.getSelection().size()
						// + " 个产品被选中)");

						if (store.indexOf(view.getSelectionModel()
								.getSelectedItem()) >= 0)

							store.gotorow(store.indexOf(view
									.getSelectionModel().getSelectedItem()));

						if (be.getSelection().size() > 0) {
							detailTp.overwrite(details.getElement(), Util
									.getJsObject(be.getSelection().get(0)));
							// chooser.getButtonById("ok").enable();
						} else {
							// chooser.getButtonById("ok").disable();
							details.el().setInnerHtml("");
						}

					}

				});

		panel.add(view);
		Tools.setResize(view,0.5,0.80);
		Tools.setResize(panel,0.4,0.80);
		return panel;
	}
	BaseFormPanel form  =null;
	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(4);
		form.setBodyBorder(false);//(false);
		form.setPadding(1);
		//form.setWidth(260);
		
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.CENTER);
		td.setVerticalAlign(VerticalAlignment.MIDDLE);
		td.setPadding(2);
		
		TableData td3 = new TableData();
		td3.setRowspan(1);
		td3.setColspan(3);
		td3.setHorizontalAlign(HorizontalAlignment.LEFT);
		td3.setVerticalAlign(VerticalAlignment.MIDDLE);
		td3.setPadding(2);
		
		TableData td2 = new TableData();
		td2.setRowspan(1);
		td2.setColspan(4);
		td2.setHorizontalAlign(HorizontalAlignment.CENTER);
		td2.setVerticalAlign(VerticalAlignment.MIDDLE);
		td2.setPadding(2);
		
		
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("备注"), td);
		TextArea C_num4 = new TextArea();
		C_num4.setHeight(360);
		C_num4.setWidth(290);
		C_num4.setName("mark");
		form.add(C_num4, td3);
		
		form.add(save,td2);
		
		form.inited();
		return form;
	};

	private native String getTemplate() /*-{
		return ['<tpl  for=".">', 
		'<div class="thumb-wrap" id="sss" style="border: 1px solid white;float:left;">',

		'<div class="thumb" ><img style="width:200px;height:200px;border:5px double #eee;margin: 3px 2px 2px 3px" src="{imagepath}" title="name"></div>', 
		'<span class="x-editable" style="float:left" >&nbsp;</span></div>', 
		'</tpl>',
		'<div class="x-clear"></div>'].join("");
	}-*/;

	public native String getDetailTemplate() /*-{
		return ['<div class="details">', 
		'<tpl for=".">', 
		'<img style="width:100px;height:100px;border:5px double #eee;margin: 3px 2px 2px 3px" src="{imagepath}"><div class="details-info">', 
		'<b></b>', 
		'<span></span>', 
		'<b></b>', 
		'<span>{mark}</span>', 
		'<b></b>', 
		'<span></span></div>', 
		'</tpl>', 
		'</div>'].join("");
	}-*/;

	@Override
	public boolean isopen() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean open() {
		// TODO Auto-generated method stub
		return false;
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(add)) {
								// SWFFileUpload swfu =new
								// SWFFileUpload("&uploadcommand=needsave&tableinftablename=bcode_extdata&imagepath=imagepath&columns=bcode,&values=123,");
								LabelField dd;
								SWFFileUpload swfu = new SWFFileUpload();
								swfu
										.setParams("&uploadcommand=needsave&tableinftablename=bcode_extdata&imagepath=imagepath&columns=bcode,&values='"+mainstore.getValue("bcode")+"',");
								swfu.showDialog();
							

							} else if (be.getSource().equals(del)) {

								if (store.getCurcowindex() >= 0) {
									store.remove(store.getAt(store
											.getCurcowindex()));
									store.trySave();
								}
							}
							else if (be.getSource().equals(save)) {
								store.trySave(store.getCurcowindex());
							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
	
	
}
